from bs4 import BeautifulSoup #
from selenium import webdriver #浏览器
import time #时间操作

#爬取dolin 商品信息
from selenium.common.exceptions import NoSuchElementException, TimeoutException


class CommentPage:
    def __init__(self):
        # self.url = "https://item.jd.com/100005727822.html#product-detail"
        self.basePath = "D://dolin"
    def start(self,dbo,url):
        print("——开始自动化爬取———")
        #浏览器设置
        options = webdriver.ChromeOptions()
        #设置中文
        options.add_argument('lang=zh_CN.UTF-8')
        #更换头部
        ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
        options.add_argument('user-agent="%s"' % ua)
        driver = webdriver.Chrome(chrome_options=options)
        while True:
            try:
                driver.get(url)
                break;
            except TimeoutException:
                continue;
        # self.scroll_win(driver,1)  #下拉刷新 1 次数
        assert "东菱" in driver.title
        time.sleep(2)
        #切换iframe
        # driver.switch_to_frame("iframe_all")
        html = BeautifulSoup(driver.page_source,'lxml')
        #去没用标签
        [a.extract() for a in html.find_all('div',class_='vjs-control-bar')]
        [a.extract() for a in html.find_all('div',class_='vjs-error-display')]
        [a.extract() for a in html.find_all('div',class_='vjs-caption-settings')]
        [a.extract() for a in html.find_all('div',class_='comment-op')]
        [a.extract() for a in html.find_all('div',class_='user-level')]
        #标题
        title = html.find('ul',class_='parameter2').li['title']
        #品牌名称
        brandname = driver.find_element_by_xpath(".//*[@clstag='shangpin|keycount|product|pinpai_1']").text
        #店铺名称
        while True:
            time.sleep(2)
            try:
                shopname  = driver.find_element_by_xpath(".//*[@clstag='shangpin|keycount|product|bbtn']").text
                break;
            except NoSuchElementException:
                continue
        #price
        price = html.find('span',class_='p-price').find('span',class_='price').text
        if not price:
            price = '0'
        #点击商品详情
        el = driver.find_element_by_xpath(".//*[@clstag='shangpin|keycount|product|pcanshutab']")
        el.click()
        time.sleep(2) #等待
        #型号
        model = "无"
        ptableItem= BeautifulSoup(driver.page_source,'lxml').find_all('div',class_='Ptable-item')
        for item in ptableItem:
            dls = item.find_all('dl',class_='clearfix')
            for dl in dls:
                if dl.dt.text == '认证型号':
                    model = dl.dd.text
        #插入数据
        goods_sql = "INSERT INTO `g_goods` (title,brandname,shopname,price,model) " \
         "VALUE (%s, %s, %s, %s, %s)"
        value =(title,brandname,shopname,price,model)
        # dbo = DbOperate()
        gid = dbo.execBySqlVal(goods_sql,value)
        if(gid==0):
            return
        #点击商品评价
        el = driver.find_element_by_xpath(".//*[@clstag='shangpin|keycount|product|shangpinpingjia_1']")
        el.click()
        self.scroll_win(driver,1)  #下拉刷新 1 次数
        all = html.find('div',id='comment-0')
        while '全部' in all.text or all==None:
            time.sleep(2) #等待
            all = BeautifulSoup(driver.page_source,'lxml').find('div',id='comment-0')
            pass
        while True:
            #获取评论信息
            commentItem = BeautifulSoup(driver.page_source,'lxml').find('div',id='comment-0').find_all('div',class_='comment-item')
            # 定义总插入行数为一个空列表
            data_list = []
            for item in commentItem:
                # 添加所有任务到总的任务列表
                data_list.append(self.saveComment(item,gid))
            #插入数据
            commment_sql = "INSERT INTO `a_assess` (gid,headurl,username,`level`,content,assesstime) " \
                           "VALUES (%s, %s, %s, %s, %s, %s)"
            num = dbo.execBySqlVals(commment_sql,data_list)
            if(num):
                break;
            try:
                driver.find_element_by_xpath(".//*[@clstag='shangpin|keycount|product|pinglunfanye-nextpage']")
            except NoSuchElementException as e:
                break;
            driver.execute_script("$('.ui-pager-next').click()")
            time.sleep(1) #等待
        # assert "abc" in driver.title
        driver.quit()
    def saveComment(self,item,gid):
        headurl = item.find('img',class_='avatar')['src'].replace('//','http://')
        username =item.find('div',class_='user-info').text.strip()
        level = 0
        if(item.find('div',"star1")):
            level=1
        elif(item.find('div',"star2")):
            level=2
        elif(item.find('div',"star3")):
            level=3
        elif(item.find('div',"star4")):
            level=4
        elif(item.find('div',"star5")):
            level=5
        content = item.find('div',class_='J-comment-column')
        assesstime = item.find('div',class_='order-info').find_all('span')
        lens = len(assesstime)
        if(lens==1):
            assesstime = assesstime[0].text.strip()
        else:
            assesstime = assesstime[1].text.strip()
        # print(headurl+username+str(level)+str(content)+assesstime)
        return (gid,headurl,username,str(level),str(content),assesstime)
    # 下拉加载
    def scroll_win(self,driver,times):
        for i in range(times):
            print("开始执行第", str(i + 1),"次下拉操作")
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")  #执行JavaScript实现网页下拉倒底部
            print("第", str(i + 1), "次下拉操作执行完毕")
            print("第", str(i + 1), "次等待网页加载......")
            time.sleep(3)  # 等待30秒，页面加载出来再执行下拉操作
# CommentPage().start(DbOperate(),"https://item.jd.com/100005727822.html")

    #加好评文字描述
# for a in html.find_all('div',class_='star1'):a.string = "1星差评";
# for a in html.find_all('div',class_='star2'):a.string = "2星差评"
# for a in html.find_all('div',class_='star3'):a.string = "3星中评"
# for a in html.find_all('div',class_='star4'):a.string = "4星好评"
# for a in html.find_all('div',class_='star5'):a.string = "5星好评"
# #设置好评属性
# for a in html.find_all('div',class_='comment-star'):a['style']='color:red;font-size:25px;'